package com.android.upload;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;
//
//import org.apache.http.HttpEntity;
//import org.apache.http.HttpResponse;
//import org.apache.http.HttpVersion;
//import org.apache.http.client.ClientProtocolException;
//import org.apache.http.client.HttpClient;
//import org.apache.http.client.methods.HttpPost;
//import org.apache.http.entity.mime.MultipartEntity;
//import org.apache.http.entity.mime.content.ContentBody;
//import org.apache.http.entity.mime.content.FileBody;
//import org.apache.http.impl.client.DefaultHttpClient;
//import org.apache.http.params.CoreProtocolPNames;
//import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class UploadDemoActivity extends Activity {

	String filename1 = "money.jpg";
	String filename2 = "file2.txt";
	String filename3 = "file3.txt";
	File file1;
	File file2;
	File file3;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// String ftpServer = "http://chomchom.us/images/upload.php";
		// String ftpServer = "http://192.168.1.120/upload.php";

		Log.i("Main", "start upload");
		// upload(ftpServer, this.getFilesDir() + "/money.jpg", "image/jpeg");
		upload();
	}

	public void upload() {
		HttpURLConnection connection = null;
		DataOutputStream outputStream = null;
		FileInputStream fileInputStream = null;

		String pathToOutFile = this.getFilesDir() + "/nature_0010.jpg";
		String urlServer = "http://chomchom.us/images/upload/upload.php";
		// String urlServer = "http://192.168.1.109/upload.php";
		// cái này chưa bít có cần ko?
		String lineEnd = "\r\n";
		String twoHyphen = "--";
		String boundary = "*****";

		int bytesRead, bytesAvailable, bufferSize;
		byte[] buffer;
		int maxBufferSize = 1024 * 1024;

		try {
			try {
				fileInputStream = new FileInputStream(new File(pathToOutFile));
			} catch (FileNotFoundException e) {
				Log.i("FileInputStream", pathToOutFile + "not found");
			}
			URL url = new URL(urlServer);

			connection = (HttpURLConnection) url.openConnection();

			// Allow input and output
			connection.setDoInput(true);
			connection.setDoOutput(true);
			connection.setUseCaches(false);

			// Enable post method
			connection.setRequestMethod("POST");

			connection.setRequestProperty("Connection", "Keep_Alive");
			connection.setRequestProperty("Content-Type",
					"multipart/form-data; boundary=" + boundary);

			outputStream = new DataOutputStream(connection.getOutputStream());
			outputStream.writeBytes(twoHyphen + boundary + lineEnd);
			outputStream
					.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
							+ pathToOutFile + "\"" + lineEnd);
			outputStream.writeBytes(lineEnd);

			bytesAvailable = fileInputStream.available();
			bufferSize = Math.min(bytesAvailable, maxBufferSize);
			buffer = new byte[bufferSize];

			bytesRead = fileInputStream.read(buffer, 0, bufferSize);

			while (bytesRead > 0) {
				outputStream.write(buffer, 0, bufferSize);
				bytesAvailable = fileInputStream.available();
				bufferSize = Math.min(bytesAvailable, maxBufferSize);
				bytesRead = fileInputStream.read(buffer, 0, bufferSize);
			}

			outputStream.writeBytes(lineEnd);
			outputStream.writeBytes(twoHyphen + boundary + twoHyphen + lineEnd);

			int serverResponseCode = connection.getResponseCode();
			String serverResponseMessage = connection.getResponseMessage();

			fileInputStream.close();
			outputStream.flush();
			outputStream.close();

			Log.i("serverResponseCode", String.valueOf(serverResponseCode));
			Log.i("serverResponseMessage", serverResponseMessage);
		} catch (IOException e) {
			Log.i("IOException", e.getMessage());
		}
	}
	/**
	 * public void upload1(String ftpServer, String filename, String filetype) {
	 * try { HttpClient httpclient = new DefaultHttpClient();
	 * httpclient.getParams().setParameter( CoreProtocolPNames.PROTOCOL_VERSION,
	 * HttpVersion.HTTP_1_1);
	 * 
	 * HttpPost httppost = new HttpPost(ftpServer); File file = new
	 * File(filename);
	 * 
	 * MultipartEntity mpEntity = new MultipartEntity(); ContentBody cbFile =
	 * new FileBody(file, filetype); mpEntity.addPart("userfile", cbFile);
	 * 
	 * httppost.setEntity(mpEntity); Log.i("Upload","executing request " +
	 * httppost.getRequestLine());
	 * 
	 * HttpResponse response = null; try { Log.i("excute", "excute"); response =
	 * httpclient.execute(httppost); Log.i("excute", "excute"); } catch
	 * (ClientProtocolException e) { // TODO Auto-generated catch block
	 * Log.i("ClientProtocolException", e.getMessage()); } catch (IOException e)
	 * { // TODO Auto-generated catch block Log.i("IOException",
	 * e.getMessage()); } Log.i("getEntity", "getEntity"); HttpEntity resEntity
	 * = response.getEntity();
	 * 
	 * Log.i("Upload",response.getStatusLine().toString()); if (resEntity !=
	 * null) { Log.i("Upload", EntityUtils.toString(resEntity)); } if (resEntity
	 * != null) { resEntity.consumeContent(); }
	 * 
	 * httpclient.getConnectionManager().shutdown(); } catch (Exception e) { //
	 * Log.i("Exception", e.getMessage()); } }
	 */
}
